//
//  HJmainViewController.swift
//  HJWeiBo
//
//  Created by 黄珏 on 15/10/26.
//  Copyright © 2015年 黄珏. All rights reserved.
//

import UIKit

class HJmainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
//        tabBar = HJTabBar() apple的私有属性，只读 所以会报错
        setValue(HJTabBar(), forKey: "tabBar")
        tabBar.tintColor = UIColor.orangeColor()
        
        let homeVC = HJHomeTableViewController()
        self.addChildViewController(homeVC, title: "主页", icon: "tabbar_home_highlighted")
        
        let messageVC = HJMessageTableViewController()
        self.addChildViewController(messageVC, title: "消息", icon: "tabbar_message_center")
        
        let discoverVC = HJDiscoverTableViewController()
        self.addChildViewController(discoverVC, title: "发现", icon: "tabbar_discover")
        
        let meVC = HJProfileTableViewController()
        self.addChildViewController(meVC, title: "我", icon: "tabbar_profile")
    }
    /**
     加载composeButton
    */
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print("viewWillAppear")
        let width = UIScreen.mainScreen().bounds.width / CGFloat(5)
        composeButton.frame = CGRectMake(2 * width,0 , width, tabBar.bounds.height)
        tabBar.addSubview(composeButton)
    }

    private func addChildViewController(controller: UIViewController,title: String,icon: String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: icon)
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    func composeButtonClick() {
        print("composeButtonClick")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /// 懒加载撰写按钮
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
}
